<?php

namespace think\wework\Api;

use GuzzleHttp\Exception\GuzzleException;
use think\wework\Abstracts\WeworkApiWithToken;

class User extends WeworkApiWithToken
{
    private static ?User $instance = null;
    public static function getInstance(): User
    {
        if(!self::$instance instanceof User) {
            self::$instance = new self();
        }
        return self::$instance;
    }
    /**
     * 创建成员
     *
     * @param array $json
     * @return array
     */
    public function create(array $json): array
    {
        return $this->httpClient->post('user/create', compact('json'))->toArray();
    }

    /**
     * 读取成员
     *
     * @param string $id
     * @return array
     */
    public function get(string $id): array
    {
        $query = ['userid' => $id];
        return $this->httpClient->get('user/get', compact('query'))->toArray();
    }

    /**
     * 更新成员
     *
     * @param array $json
     * @return array
     */
    public function update(array $json): array
    {
        return $this->httpClient->post('user/update', compact('json'))->toArray();
    }

    /**
     * 删除成员
     *
     * @param string $id
     * @return array
     * @throws GuzzleException
     */
    public function delete(string $id): array
    {
        $query = ['userid' => $id];
        return $this->httpClient->get('user/delete', compact('query'))->toArray();
    }

    /**
     * 批量删除成员
     *
     * @param array $idList
     * @return array
     * @throws GuzzleException
     */
    public function batchDelete(array $idList): array
    {
        $json = ['useridlist' => $idList];
        return $this->httpClient->post('user/batchdelete', compact('json'))->toArray();
    }

    /**
     * 获取部门成员
     *
     * @param int $departmentId 部门ID
     * @param bool $fetchChild 是否同步下级部门
     * @param bool $needDetail 是否获取成员详情
     * @return array
     * @throws GuzzleException
     */
    public function list(int $departmentId, bool $fetchChild = false, bool $needDetail = false): array
    {
        $uri = 'user/' . ($needDetail ? 'list' : 'simplelist');

        $query = ['department_id' => $departmentId, 'fetch_child' => (int)$fetchChild];
        return $this->httpClient->get($uri, compact('query'))->toArray();
    }

    /**
     * userid转openid
     *
     * @param string $id
     * @return array
     */
    public function convertIdToOpenid(string $id): array
    {
        $json = ['userid' => $id];
        return $this->httpClient->post('user/convert_to_openid', compact('json'))->toArray();
    }

    /**
     * openid转userid
     *
     * @param string $openid
     * @return array
     */
    public function convertOpenidToUserId(string $openid): array
    {
        $json = ['openid'=>$openid];
        return $this->httpClient->post('user/convert_to_userid', compact('json'))->toArray();
    }

    /**
     * 二次验证
     *
     * @param string $id
     * @return array
     */
    public function authSuccess(string $id): array
    {
        $query = ['userid' => $id];
        return $this->httpClient->get('user/authsucc', compact('query'))->toArray();
    }

    /**
     * 根据code获取成员信息
     *
     * @param string $code
     * @return array
     */
    public function getInfo(string $code): array
    {
        $query = ['code' => $code];
        return $this->httpClient->get('user/getuserinfo', compact('query'))->toArray();
    }

    /**
     * 使用user_ticket获取成员详情
     *
     * @param string $ticket
     * @return array
     */
    public function getDetail(string $ticket): array
    {
        $json = ['user_ticket' => $ticket];
        return $this->httpClient->post('user/getuserdetail', compact('json'))->toArray();
    }
}
